<?php
uses('controller');

class clickController extends Controller {

    function __construct() {
        parent::__construct();
    }

    function click() {
        /*
        url:要转向的url
        table:要更新的表名及字段 table|field
        sig:签名
        */
        if($this->request['url']) {
            $md5 = md5(serialize(array($this->request['id'], $this->request['table'])));
            $sig = $this->sigGen($this->session['sig']['click'][$md5], $this->request['id'], $this->request['table']);

            if($this->request['sig'] == $sig) {
                //记录点击
                unset($this->session['sig']['click'][$md5]);
                $data['id']     = $this->request['id'];
                $data['table']  = $this->request['table'];
                $this->model->click($data);
            }

            header("Location:".urldecode($this->request['url']));
        } else {
            header("Location:/");
        }
    }

    //生成签名
    function sigGen($sig="") {
        $arr = func_get_args();
        array_shift($arr);

        //密钥
        if(!$sig) {
            $tmp = md5(serialize($arr));
            $_SESSION['sig']['click'][$tmp] = rand(1000, 999999);
            $sig = $_SESSION['sig']['click'][$tmp];
        }

        $arr['sig'] = $sig;
        sort($arr);

        return md5(serialize($arr));
    }
}
?>